package com.anjubao.parking.municipal.parkinglot.service;

import com.anjubao.parking.municipal.basezone.dto.DistrictsResDto;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingStatFailuresDevicesDto;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingStatFailuresZonesDto;
import com.anjubao.parking.municipal.parkinglot.mapper.BaseZoneMapper;
import com.anjubao.parking.municipal.parkinglot.mapper.ParkingStatFailuresMapper;
import com.anjubao.parking.municipal.parkinglot.model.ParkingLotDeviceLog;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author anjubao
 */
@Service
public class ParkingStatFailuresService extends ServiceImpl<ParkingStatFailuresMapper,ParkingLotDeviceLog> {

    @Autowired
    private ParkingStatFailuresMapper parkingStatFailuresMapper;
    @Autowired
    private BaseZoneMapper baseZoneMapper;
    private static final String DISTRICT_ID="all";

    /**
     * 各区域运维事件排名/故障设备占比统计
     * @param tab
     * @param cityId
     * @return
     */
    public List<ParkingStatFailuresZonesDto> getFailuresRecordsZones(Integer cityId,String tab) {
        //查询数据库运维事件
        List<ParkingStatFailuresZonesDto> failuresRecordsZonesList=parkingStatFailuresMapper.getFailuresRecordsZones(cityId,tab);
        //查询城市全部区域
        if (DISTRICT_ID.equals(tab)){
            List<DistrictsResDto> districts = baseZoneMapper.findDistricts(cityId);
            //遍历区域list
            for (DistrictsResDto district : districts) {
                ParkingStatFailuresZonesDto parkingStatFailuresZones=new ParkingStatFailuresZonesDto();
                //得到所有区域name
                String districtName =district.getDistrictName();
                Boolean flat = false;
                //遍历数据库中的区域运维事件
                for (ParkingStatFailuresZonesDto parkingStatFailuresZonesDto : failuresRecordsZonesList) {
                    //得到区域名称
                    String name = parkingStatFailuresZonesDto.getDistrictName();
                    if (name.equals(districtName)){
                        flat=true;
                        break;
                    }
                }
                if (!flat){
                    parkingStatFailuresZones.setDistrictName(districtName);
                    parkingStatFailuresZones.setFailuresNum(0);
                    failuresRecordsZonesList.add(parkingStatFailuresZones);
                }
            }
        }
        //将运维事件排序
        Collections.sort(failuresRecordsZonesList, new Comparator<ParkingStatFailuresZonesDto>() {
            @Override
            public int compare(ParkingStatFailuresZonesDto o1, ParkingStatFailuresZonesDto o2) {
                return o1.getFailuresNum()-o2.getFailuresNum();
            }
        });
        return failuresRecordsZonesList;
    }

    /**
     * 设备运维事件占比统计
     * @return
     */
    public List<ParkingStatFailuresDevicesDto> getFailuresRecordsDevices(Integer cityId) {
        return parkingStatFailuresMapper.getFailuresRecordsDevices(cityId);
    }
}
